﻿using System.Data.SQLite;
using System.Collections.Generic;

namespace GrandExchangeTool
{
    class IdProvider
    {
        readonly SQLiteConnection _db;

        public IdProvider(string databaseFile)
        {
            _db = new SQLiteConnection("Data Source=" + databaseFile + ";Version=3;");
            _db.Open();
        }

        public int GetId(string name)
        {
            var data = new SQLiteCommand("SELECT Id FROM Items WHERE Name='" + name + "';", _db).ExecuteReader();
            data.Read();
            try
            {
                return data.GetInt32(0);
            }
            catch
            {
                return -1;
            }
        }
        public List<string> GetItems(string search)
        {
            List<string> result = new List<string>();
            var data = new SQLiteCommand("SELECT Name FROM Items WHERE Name LIKE '%" + search
                 + "%' ORDER BY Name ASC, Name LIKE '" + search + "%' ASC;", _db).ExecuteReader();
            while (data.Read())
            {
                result.Add(data.GetString(0));
            }

            return result;
        }
    }
}
